package com.lancoo.cvas.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.lancoo.cvas.pojo.vo.Building;
import com.lancoo.cvas.pojo.vo.RoomVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 教室
 *
 * @author liufeng
 * @email 1037383243@qq.com
 * @date 2023-12-04 11:54:00
 */
@Data
@TableName("room")
@Accessors(chain = true)
@ApiModel("教室信息")
public class Room implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 教室id
     */
    @TableId(value = "room_id", type = IdType.AUTO)
    @ApiModelProperty("教室id")
    private Long roomId;
    /**
     * 任务id
     */
    @ApiModelProperty("任务id")
    private Integer taskId;
    /**
     * 基础平台教室id
     */
    @ApiModelProperty("基础平台教室id")
    private Integer baseRoomId;
    /**
     * 教室名称
     */
    @ApiModelProperty("教室名称")
    private String roomName;
    /**
     * 第几层
     */
    @ApiModelProperty("第几层")
    private Integer buildingFloor;
    /**
     * 建筑id
     */
    @ApiModelProperty("建筑id")
    private Integer buildingId;
    /**
     * 建筑名称
     */
    @ApiModelProperty("建筑名称")
    private String buildingName;
    /**
     * 教室类型
     */
    @ApiModelProperty("教室类型")
    private Integer roomType;
    /**
     * 容量（不可为空）
     */
    @ApiModelProperty("容量")
    private Integer roomNum;
    /**
     * 所属校区
     */
    @ApiModelProperty("所属校区")
    private String campusId;
    /**
     * 所属校区名称
     */
    @ApiModelProperty("所属校区名称")
    private String campusName;
    /**
     * 学校id
     */
    @ApiModelProperty("学校id")
    private String schoolId;
    /**
     * 学期
     */
    @ApiModelProperty("学期")
    private String term;
    /**
     * 是否可用 -1为已删除 0为不可用 1为可用 2为用户创建的教室
     */
    @ApiModelProperty("是否可用")
    private Integer isEnable;
    /**
     * 删除标识：
     * 0：未删除
     * 1：删除
     */
    @ApiModelProperty("删除标识")
    private Integer deleted;
    /**
     *
     */
    private String reserve;

    @TableField(exist = false)
    @ApiModelProperty("排课状态")
    private Boolean scheduleState = true;

    public static List<RoomVo> toRoomTree(List<Room> roomList) {

        List<RoomVo> campusList = new ArrayList<>();

        for (Room room : roomList) {
            String campusId = room.getCampusId();
            String campusName = room.getCampusName();

            RoomVo campus = null;
            for (RoomVo c : campusList) {
                if (c.getCampusId().equals(campusId)) {
                    campus = c;
                    break;
                }
            }
            if (campus == null) {
                campus = new RoomVo();
                campus.setCampusId(campusId);
                campus.setCampusName(campusName);
//                campus.setRoomSum(campus.getRoomSum() + 1);
                campus.setBuildings(new ArrayList<Building>());
                campusList.add(campus);
            }

            int buildingId = room.getBuildingId();
            String buildingName = room.getBuildingName();
            Building building = null;
            for (Building b : campus.getBuildings()) {
                if (b.getBuildingId() == buildingId) {
                    building = b;
                    break;
                }
            }
            if (building == null) {
                building = new Building();
                building.setBuildingId(buildingId);
                building.setBuildingName(buildingName);
//                building.setRoomSum(building.getRoomSum() + 1);
                building.setRooms(new ArrayList<Room>());
                campus.getBuildings().add(building);
            }

            building.getRooms().add(room);
            building.setRoomSum(building.getRoomSum() + 1);
            campus.setRoomSum(campus.getRoomSum() + 1);

            building.setScheduleState(room.getScheduleState()? building.getScheduleState() : false);
            campus.setScheduleState(room.getScheduleState()? campus.getScheduleState() : false);
        }
        return campusList;
    }
}
